package com.hnevc.xiaoming.httpactivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.adapter.MyWeatherRvAdapter;
import com.hnevc.xiaoming.bean.MyWeatherBean;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MyWeatherActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView title_view;
    private EditText et_city;
    private Button btn_qst;
    private TextView weather_date;
    private TextView tv_city;
    private TextView tv_info;
    private TextView tv_direct;
    private TextView tv_power;
    private LinearLayout l_main;
    private String city = "长沙";
    private RecyclerView weather_recyclerview;
    private String url;
    private boolean submit;

    private static final String TAG = "MyWeatherActivity";
    private MyWeatherBean myWeatherBean;
    private ArrayList<MyWeatherBean.ResultBean.FutureBean> beans;
    private TextView zhuangtai;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_weather);
        initView();
        initData();
    }

    private void initData() {
            url = "http://apis.juhe.cn/simpleWeather/query?key=77914efee0ce95445578f293f3073e76&city=" + city;

//        JSONObject jsonObject = new JSONObject();
//        try {
//
//            if (submit = true) {
//                jsonObject.put("city",city);
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        RequestQueue requestQueue = Volley.newRequestQueue(MyWeatherActivity.this);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Log.e(TAG, "onResponse: " + jsonObject.toString());
                Gson gson = new Gson();
                myWeatherBean = gson.fromJson(jsonObject.toString(), MyWeatherBean.class);
                if (myWeatherBean.getResult() != null) {
                    setView();
                }else {
                    Toast.makeText(MyWeatherActivity.this,"请输入正确的城市信息！",Toast.LENGTH_LONG).show();
                }
            }

            private void setView() {

                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format1 = format.format(date.getTime());
                Log.e(TAG, "setView: " + format1);
                weather_date.setText("当前查询时间: " + format1);


                tv_city.setText("当前城市: " + myWeatherBean.getResult().getCity());
                tv_direct.setText("当前风向: " + myWeatherBean.getResult().getRealtime().getDirect());
                tv_info.setText("当前天气: " + myWeatherBean.getResult().getRealtime().getInfo());
                tv_power.setText("当前风级: " + myWeatherBean.getResult().getRealtime().getPower());

                beans = new ArrayList<>();
                beans.addAll(myWeatherBean.getResult().getFuture());
                MyWeatherRvAdapter rvAdapter = new MyWeatherRvAdapter(MyWeatherActivity.this, beans);
                weather_recyclerview.setAdapter(rvAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MyWeatherActivity.this, "网络请求失败！", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);


    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        title_view = (TextView) findViewById(R.id.title_view);
        title_view.setText("天气预报");

        et_city = (EditText) findViewById(R.id.et_city);
        btn_qst = (Button) findViewById(R.id.btn_qst);

        weather_date = (TextView) findViewById(R.id.weather_date);
        tv_city = (TextView) findViewById(R.id.tv_city);
        tv_info = (TextView) findViewById(R.id.tv_info);
        tv_direct = (TextView) findViewById(R.id.tv_direct);
        tv_power = (TextView) findViewById(R.id.tv_power);
        l_main = (LinearLayout) findViewById(R.id.l_main);

        btn_qst.setOnClickListener(this);
        weather_recyclerview = (RecyclerView) findViewById(R.id.weather_recyclerview);
        weather_recyclerview.setLayoutManager(new LinearLayoutManager(MyWeatherActivity.this, RecyclerView.HORIZONTAL, false));

        zhuangtai = (TextView) findViewById(R.id.zhuangtai);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_qst:
                submit = submit();
                if (submit == true) {
                    initData();
                }
                break;
        }
    }

    private boolean submit() {
        // validate
        city = et_city.getText().toString().trim();
        if (TextUtils.isEmpty(city)) {
            Toast.makeText(this, "city不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}